//
//  WBNetworkManager+Extension.swift
//  微博
//
//  Created by 康智桐 on 2018/4/13.
//  Copyright © 2018年 Kk. All rights reserved.
//

import Foundation


//封装新浪微博的网络请求方法
extension WBNetworkManager{
    
    
    
    func statusList(since_id:Int64 = 0,max_id:Int64 = 0, completion:@escaping (_ list:[[String:AnyObject]]?,_ isSuccess:Bool)->()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["since_id":"\(since_id)","max_id":"\(max_id > 0 ? max_id - 1 : 0)"]
        
        
        tokenRequest(URLString: urlString, parameters: params){
            (json,isSuccess) in
            
            let result = (json as! NSDictionary)["statuses"] as? [[String : AnyObject]]
            
            completion(result,isSuccess)
            
        }
    }
    
    //返回微博的未读数量
    func unreadCount( completion:@escaping (_ count:Int)->()){
        
        guard  let uid = userAccount.uid  else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        let params = ["uid":uid]
        
        tokenRequest(URLString: urlString, parameters: params) {(json, isSuccess) in
            
            let dict = json as? [String:Any]
            let count = dict?["status"] as? Int
            
           completion(count ?? 0)
        
        }
        
    }
    
}

// MARK: - 发布微博
extension WBNetworkManager{
    
    func postStatus(text: String,completion: @escaping (_ result: [String: Any]?, _ isSuccess: Bool)->()) -> (){
        
        
        // 1. url       
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        // 2. 参数字典
        let params = ["status": text]
        
        
       // 起网络请求
        tokenRequest(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            completion((json as? [String: Any]), isSuccess)
        }

        
    }
    
    
}

//用户信息
extension WBNetworkManager{
    //加载当前用户信息
    func loadUserInfo(completion:@escaping (_ dict:[String:Any])->()){
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let params = ["uid":uid]
        
        //发起网络请求
        tokenRequest(URLString: urlString, parameters: params){ (json,isSuccess) in
            //print(json)
            completion(json as? [String:Any] ?? [:])
            
        }
        
        
    }
}


//OAuth 相关方法
extension WBNetworkManager{
    
    //获取token
    func loadAccessToken(code:String,completion:@escaping (_ isSuccess:Bool)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":WBAppKey,
                      "client_secret":WBAppSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WBRedirectURI]
        
        //发起网络请求
        request(method: .POST, URLString: urlString, parameters: params){(json,isSuccess) in
           // print(json)
            
            //直接使用字典设置userAccount 的属性
            self.userAccount.yy_modelSet(with: (json as? [String:Any]) ?? [:])
            
//            print(self.userAccount)
//            print("========")
            
            //加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
               // print(dict)
                
                //使用用户信息字典设置用户账户信息 用户昵称和头像地址
            self.userAccount.yy_modelSet(with: dict)
            //print(self.userAccount)
                
                //保存模型
            self.userAccount.saveAccount()
                
                //完成回调
                completion(isSuccess)
            })
            
            
            
        }
    }
    
}
